package com.mytijian.code.utils;

/**
 * Created by shjh on 2017/9/21.
 */
public class CodeUtils {
    /**
     * code组成元素正向列表
     */
    private static final char[] elements = {'2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    private static final int elements_count = 32;
    private static final int code_length = 8;

    /**
     * 生成码
     *
     * @return
     */
    public static String createCode() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < code_length; i++) {
            sb.append(elements[getRandomNumber(0, 32)]);
        }
        return sb.toString();
    }

    /**
     * 获得某个范围内的随机数
     *
     * @param num1 起始范围参数
     * @param num2 终止范围参数
     * @return 返回num1到num2的随机数
     */
    public static int getRandomNumber(int num1, int num2) {
        return (int) (num1 + Math.random() * (num2 - num1));
    }

    public static void main(String[] args) {
//        long start = System.currentTimeMillis();
//        List<String> codes = new ArrayList<>();
//        for (int i = 0; i < 1000000; i++) {
//            codes.add(createCode());
//        }
//        System.out.println(codes.size());
//        System.out.println(new HashSet<>(codes).size());
//        System.out.println((System.currentTimeMillis() - start));

        for (int i = 0; i < 20; i++) {
            System.out.println(createCode());
        }
    }
}
